home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / C and C++ / GNU Chess 3.0 / Sources TC 4.0 / MyDragMgr.c < prev    next >
Text File  |  1991-04-23  |  10KB  |  321 lines

  1. /*
  2.   Mac interface for GNU Chess
  3.  
  4.   Revision: 10 Feb 1991
  5.  
  6.   Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
  7.   Copyright (c) 1991  Airy ANDRE
  8.  
  9.     expanded game save, list, and restore features
  10.     optional auto-updating of positional information
  11.  
  12.   This file is part of CHESS.
  13.  
  14.   CHESS is distributed in the hope that it will be useful,
  15.   but WITHOUT ANY WARRANTY.  No author or distributor
  16.   accepts responsibility to anyone for the consequences of using it
  17.   or for whether it serves any particular purpose or works at all,
  18.   unless he says so in writing.  Refer to the CHESS General Public
  19.   License for full details.
  20.  
  21.   Everyone is granted permission to copy, modify and redistribute
  22.   CHESS, but only under the conditions described in the
  23.   CHESS General Public License.   A copy of this license is
  24.   supposed to have been given to you along with CHESS so you
  25.   can know your rights and responsibilities.  It should be in a
  26.   file named COPYING.  Among other things, the copyright notice
  27.   and this notice must be preserved on all copies.
  28.   */
  29.  
  30. /* This file is based on DragManager by Boyd Scott - MacTutor Vol.3 No.7 */
  31.  
  32. #include "DragMgr.h"
  33. #include <MacProto.h>
  34.  
  35.  
  36. #define Shadow_x 4
  37. #define Shadow_y 6
  38.  
  39. ShadowRecord ShadowStuff;
  40. BitMap OffScreenBits;
  41.  
  42. static GrafPtr wMgrPort,OldPort, OffPort;
  43. static RgnHandle UpdateRegion;
  44. static Rect LastRect,OtherUpdateRect,UpdateRect,DragRect,tempBounds, globRect;
  45. static Point  MousePt,OtherMousePt,TestPt;
  46. static long  SynchCount;
  47.   
  48.  
  49. static Ptr NewBitMap(BitMap *theBitMap, Rect *theRect)
  50. {
  51.      theBitMap->rowBytes=((theRect->right-theRect->left+15)/16)*2;
  52.      theBitMap->baseAddr=NewPtr(theBitMap->rowBytes
  53.                                      *(theRect->bottom-theRect->top));
  54.      theBitMap->bounds.top =  theRect->top;
  55.      theBitMap->bounds.bottom =  theRect->bottom;
  56.      theBitMap->bounds.left =  theRect->left;
  57.      theBitMap->bounds.right =  theRect->right;
  58.      if (MemError()!=noErr) return 0L;
  59.      return theBitMap->baseAddr;
  60. }
  61.  
  62. Boolean InitDrag(BitMapPtr UserOffScreenBits, Rect * Taille)
  63. {
  64.   int temp = 0;
  65.   
  66.   globRect = *Taille;
  67.   GetPort(&OldPort);
  68.   if (UserOffScreenBits)
  69.           OffScreenBits = *UserOffScreenBits;
  70.      else
  71.         if (!NewBitMap(&OffScreenBits,Taille)) return 0;
  72.   OffPort = (GrafPtr)NewPtr(sizeof(GrafPort));
  73.   if (!OffPort) 
  74.   {
  75.           DisposPtr(OffScreenBits.baseAddr);
  76.          return 0;
  77.   }
  78.   OpenPort(OffPort);
  79.   RectRgn(OldPort->clipRgn,&(**OldPort->clipRgn).rgnBBox);
  80.   UpdateRegion = NewRgn();
  81.   if (!UpdateRegion) 
  82.   {
  83.           DisposPtr(OffScreenBits.baseAddr);
  84.          ClosePort(OffPort);
  85.          DisposPtr(OffPort);
  86.          SetPort(OldPort);
  87.          return 0;
  88.   }
  89.     
  90.   
  91.   ShadowStuff.visible = 1;
  92.   ShadowStuff.dx = Shadow_x;
  93.   ShadowStuff.dy = Shadow_y;
  94.   ShadowStuff.CopyMode=srcOr;
  95.    
  96.   CopyBits(&OldPort->portBits, &OffScreenBits, Taille, Taille,
  97.                       srcCopy, OldPort->visRgn);
  98.   ClipRect(Taille);
  99.   SetPort(OldPort);
  100.   return 1;
  101. }
  102.  
  103. static Boolean TestNil(Ptr theThing, DragHandle DragStuff)
  104. {
  105.   if (!theThing)
  106.   {
  107.       DisposeDraggable(DragStuff);
  108.      SetPort(OldPort);
  109.      return 1;
  110.   }
  111.   return 0;
  112. }
  113.  
  114. Boolean NewDraggable(PicHandle thePicture, 
  115.                             PicHandle theMask,
  116.                             BitMapPtr UserPicBits,
  117.                             BitMapPtr UserShadowBits,
  118.                             DragHandle *DragStuff)
  119. {
  120.   GetPort(&OldPort);
  121.   SetPort(OffPort);
  122.   SetPortBits(&OffScreenBits);
  123.   *DragStuff = (DragHandle)NewHandle(sizeof(DragRecord));
  124.   if (!(*DragStuff))
  125.   {
  126.          SetPort(OldPort);
  127.          return 0;
  128.   }
  129.   
  130.   MoveHHi((Handle)*DragStuff);
  131.   HLock((Handle)*DragStuff);
  132.   
  133.   tempBounds = (**thePicture).picFrame;
  134.   if (UserPicBits) (***DragStuff).PictureBits = *UserPicBits;
  135.      else if (TestNil((Ptr)NewBitMap(&(***DragStuff).PictureBits,&tempBounds),
  136.                  *DragStuff)) return 0;
  137.   OffsetRect(&tempBounds,-tempBounds.left,-tempBounds.top);
  138.   if (TestNil(NewBitMap(&(***DragStuff).UnderBits,&tempBounds),*DragStuff))  return 0;
  139.   tempBounds = (**theMask).picFrame;
  140.  
  141.   if (UserShadowBits) (***DragStuff).ShadowBits = *UserShadowBits;
  142.      else if (TestNil((Ptr)NewBitMap(&(***DragStuff).ShadowBits,&tempBounds),*DragStuff)) return 0;
  143.   OffsetRect(&tempBounds,-tempBounds.left,-tempBounds.top);
  144.   if (TestNil((Ptr)NewBitMap(&(***DragStuff).UnderShadowBits,&tempBounds),*DragStuff))  return 0;
  145.   SetPortBits(&(***DragStuff).PictureBits);
  146.   EraseRect(&(***DragStuff).PictureBits.bounds);
  147.      
  148.   (***DragStuff).ShadowRegion = NewRgn();
  149.   if (TestNil((Ptr)&(***DragStuff).ShadowRegion,*DragStuff)) return 0;
  150.   (***DragStuff).ThePictureRgn = NewRgn();
  151.   if (TestNil((Ptr)(***DragStuff).ThePictureRgn,*DragStuff)) return 0;
  152.  
  153.   PenMode(patCopy);
  154.   OpenRgn();
  155.   HLock((Handle)thePicture);
  156.   DrawPicture(thePicture,&(**thePicture).picFrame);
  157.   HUnlock((Handle)thePicture);
  158.   CloseRgn((***DragStuff).ThePictureRgn);
  159.  
  160.   if (EmptyRgn((***DragStuff).ThePictureRgn))
  161.              RectRgn((***DragStuff).ThePictureRgn,&(**thePicture).picFrame);
  162.   SetPortBits(&(***DragStuff).PictureBits);
  163.   PenMode(patCopy);
  164.   HLock((Handle)thePicture);
  165.   DrawPicture(thePicture,&(**thePicture).picFrame);
  166.   HUnlock((Handle)thePicture);
  167.      
  168.   SetPortBits(&(***DragStuff).ShadowBits);
  169.   PenMode(patCopy);
  170.   EraseRect(&(***DragStuff).ShadowBits.bounds);
  171.  
  172.   CopyRgn((***DragStuff).ThePictureRgn,(***DragStuff).ShadowRegion);
  173.  
  174.   HLock((Handle)theMask);
  175.   DrawPicture(theMask,&(**theMask).picFrame);
  176.   HUnlock((Handle)theMask);
  177.      
  178.   OffsetRgn((***DragStuff).ShadowRegion,ShadowStuff.dx,ShadowStuff.dy);
  179.   DragRect = (***DragStuff).PictureBits.bounds;
  180.   SetRect(&LastRect,0,0,0,0);
  181.  
  182.   HUnlock((Handle)DragStuff);
  183.   SetPort(OldPort);
  184.   return 1;
  185. }
  186.  
  187. void DragItTo(DragHandle DragStuff, Point MousePt, Boolean Centered, Boolean Last)
  188. {
  189.     GetPort(&OldPort);
  190.  
  191.     SetPort(OffPort);
  192.     
  193.     MoveHHi((Handle)DragStuff);
  194.     HLock((Handle)DragStuff);
  195.  
  196.  
  197.     OtherMousePt = MousePt;
  198.     OffsetRgn((**DragStuff).ThePictureRgn,-DragRect.left,-DragRect.top);
  199.     OffsetRgn((**DragStuff).ShadowRegion,-DragRect.left,-DragRect.top);
  200.     OffsetRect(&DragRect,-DragRect.left,-DragRect.top); 
  201.     if (Centered) 
  202.     {
  203.         MousePt.h = MousePt.h-DragRect.right/2;
  204.          MousePt.v = MousePt.v-DragRect.bottom/2;
  205.     }
  206.   OffsetRect(&DragRect,MousePt.h,MousePt.v);
  207.   OffsetRgn((**DragStuff).ThePictureRgn,MousePt.h,MousePt.v);
  208.   OffsetRgn((**DragStuff).ShadowRegion,MousePt.h,MousePt.v);
  209.   
  210.   if (ShadowStuff.visible)
  211.   {
  212.       OffsetRect(&DragRect,ShadowStuff.dx,ShadowStuff.dy);
  213.       if (SectRect(&globRect,&DragRect,&UpdateRect))  {};
  214.       OtherUpdateRect=UpdateRect;
  215.       OffsetRect(&OtherUpdateRect,-OtherUpdateRect.left,-OtherUpdateRect.top);
  216.       CopyBits(&OffScreenBits,&(**DragStuff).UnderShadowBits,&UpdateRect,&OtherUpdateRect,
  217.                           srcCopy,0L);
  218.       OffsetRect(&DragRect,-ShadowStuff.dx,-ShadowStuff.dy);
  219.   }
  220.   
  221.   if (SectRect(&globRect,&DragRect,&UpdateRect))  {};
  222.   OtherUpdateRect=UpdateRect;
  223.   OffsetRect(&OtherUpdateRect,-OtherUpdateRect.left,-OtherUpdateRect.top);
  224.   CopyBits(&OffScreenBits,&(**DragStuff).UnderBits,&UpdateRect,&OtherUpdateRect,
  225.                       srcCopy,0L);
  226.     
  227.   if (ShadowStuff.visible)  {
  228.       OffsetRect(&DragRect,ShadowStuff.dx, ShadowStuff.dy);
  229.       CopyBits(&(**DragStuff).ShadowBits, &OffScreenBits,
  230.                               &(**DragStuff).ShadowBits.bounds, &DragRect,
  231.                              ShadowStuff.CopyMode, 0L /*(*OldPort).clipRgn*/);
  232.      OffsetRect(&DragRect,-ShadowStuff.dx,-ShadowStuff.dy);
  233.   }
  234.   
  235.   CopyBits(&(**DragStuff).ShadowBits,&OffScreenBits,
  236.                       &(**DragStuff).ShadowBits.bounds, &DragRect,
  237.                       srcOr, 0L/*(*OldPort).clipRgn*/);
  238.   CopyBits(&(**DragStuff).PictureBits,&OffScreenBits,
  239.                       &(**DragStuff).PictureBits.bounds,&DragRect,
  240.                       notSrcBic,(**DragStuff).ThePictureRgn);
  241.  
  242.   UnionRect(&DragRect,&LastRect,&UpdateRect);
  243.   
  244.   if (ShadowStuff.visible)  {
  245.     OffsetRect(&DragRect,ShadowStuff.dx,ShadowStuff.dy);
  246.       UnionRect(&DragRect,&UpdateRect,&UpdateRect);
  247.     OffsetRect(&DragRect,-ShadowStuff.dx,-ShadowStuff.dy);
  248.   }
  249.   if( SectRect(&globRect,&UpdateRect,&UpdateRect))  {};
  250.   RectRgn(UpdateRegion,&UpdateRect);
  251.   
  252.   SetPort(OldPort);
  253.   SynchCount = Ticks;
  254.   while (Ticks==SynchCount);
  255.   
  256.   CopyBits(&OffScreenBits,&OldPort->portBits,&UpdateRect,&UpdateRect,
  257.                       srcCopy,0L /*OldPort->clipRgn*/);
  258.  
  259.   if (!Last) {
  260.       SetPort(OffPort);
  261.     
  262.       if (SectRect(&DragRect,&OffScreenBits.bounds,&UpdateRect))  {};
  263.       OtherUpdateRect=UpdateRect;
  264.       OffsetRect(&OtherUpdateRect,-OtherUpdateRect.left,-OtherUpdateRect.top);
  265.       CopyBits(&(**DragStuff).UnderBits,&OffScreenBits,&OtherUpdateRect,&UpdateRect,
  266.                           srcCopy,0L /*OldPort->clipRgn*/);
  267.       LastRect=DragRect;
  268.       if (ShadowStuff.visible)  {
  269.           OffsetRect(&DragRect,ShadowStuff.dx,ShadowStuff.dy);
  270.           if (SectRect(&DragRect,&OffScreenBits.bounds,&UpdateRect))  {};
  271.           OtherUpdateRect=UpdateRect;
  272.           OffsetRect(&OtherUpdateRect,-OtherUpdateRect.left,-OtherUpdateRect.top);
  273.           CopyBits(&(**DragStuff).UnderShadowBits,&OffScreenBits,&OtherUpdateRect,
  274.                       &UpdateRect, srcCopy, 0L /*(*OldPort).clipRgn*/);
  275.           UnionRect(&LastRect,&DragRect,&LastRect);
  276.           OffsetRect(&DragRect,-ShadowStuff.dx,-ShadowStuff.dy);  
  277.       }
  278.         SetPort(OldPort);
  279.     }
  280.     HUnlock((Handle)DragStuff);
  281. }
  282.  
  283. void DisposeDraggable(DragHandle DragStuff)
  284. {
  285.     GetPort(&OldPort);
  286.     SetPort(OffPort);
  287.     SetPortBits(&OffScreenBits);
  288.     if (DragStuff)  {
  289.         HLock((Handle)DragStuff);
  290.           if (OffScreenBits.baseAddr);
  291.           
  292.           if ((**DragStuff).PictureBits.baseAddr)  DisposPtr((**DragStuff).PictureBits.baseAddr);
  293.           if ((**DragStuff).UnderBits.baseAddr)  DisposPtr((**DragStuff).UnderBits.baseAddr);
  294.           if ((**DragStuff).UnderShadowBits.baseAddr)  DisposPtr((**DragStuff).UnderShadowBits.baseAddr);
  295.           if ((**DragStuff).ShadowBits.baseAddr)  DisposPtr((**DragStuff).ShadowBits.baseAddr);
  296.           if ((**DragStuff).ThePictureRgn)  DisposHandle((Handle)(**DragStuff).ThePictureRgn);
  297.           if ((**DragStuff).ShadowRegion)  DisposHandle((Handle)(**DragStuff).ShadowRegion);
  298.         HUnlock((Handle)DragStuff);
  299.         DisposHandle((Handle)DragStuff);
  300.     }
  301.     SetPort(OldPort);
  302. }
  303.  
  304. void UpdateOffScreen(void)
  305. {
  306.     SetPort(OffPort);
  307.     SetPortBits(&OffScreenBits);
  308. }
  309.  
  310. void CloseDrag (Boolean DisposeBitMap)
  311. {
  312.     if (OffPort)
  313.     {
  314.         ClosePort(OffPort);
  315.           DisposPtr(OffPort);
  316.     }
  317.     if (UpdateRegion)  DisposHandle((Handle)UpdateRegion);
  318.     if (DisposeBitMap)
  319.         if (OffScreenBits.baseAddr)  DisposPtr(OffScreenBits.baseAddr);
  320. }
  321.